import { App, epclass, navigate } from '../../../library/api/index'
Page({
  data: {
    class: {
      id: null,
      like_count: 0
    },
    is_living: false,
    navIndex: 'on',
    tip: 'blank',
    tipData: {}
  },
  onLoad: function(e) {
    this.setData({
      class: {
        id: e.cid
      }
    })
    if(e.from !== undefined && e.from == 'me') {
      this.setData({
        navIndex: 'off',
        navMe: 'on'
      })
    }
    App.jice.onLoad(e)
  },
  onReady: function() {
    App.jice.track('page', {
      pagepath: '/pages/epclass/view/index',
      pageTitle: '预约',
      pageType: 'view'
    })
    App.logger({
      type: App.G.view,
      channel: App.G.channel,
      target: '课程预约',
      class_id: this.data.class.id
    })
  },
  onShow: function() {
    this.get()
    App.jice.track('appshow')
  },
  get: function() {
    epclass.getClass({cid: this.data.class.id}, r => {
      if(r.code == 200) {
        if(r.data.status == 8) {
          this.showGoIndexTip('正在生成回顾，敬请期待~')
        }
        if(r.data.status == 7) {
          this.goReview()
        }
        if(r.data.status == 10) {
          this.data.is_living = true
        }
        this.setData({
          class: r.data,
          is_living: this.data.is_living
        })
      }
    })
  },
  like: function() {
    epclass.like({cid: this.data.class.id}, r => {
      if(r.code == 200) {
        this.data.class.like_count = r.like_count
        this.setData({
          class: this.data.class
        })
      } else {
        this.showErrorTip(r.message)
      }
    })
  },
  sign: function(e) {
    var post = {
      form_id: e.detail.formId,
      cid: this.data.class.id
    }
    epclass.apply(post, r => {
      if(r.code == 202) {
        this.showGoIndexTip(r.msg)
      } else if(r.code == 200) {
        this.data.class.is_apply = true
        this.showApplySuccess()
      } else if(r.code == 303) {
        this.showGoLiveTip(r.msg)
      } else if(r.code == 201) {
        this.data.class.is_apply = true
        this.showGoRegisterTip()
      } else {
        this.showErrorTip(r.msg)
      }
    })
  },
  viewExpert: function() {
    this.setData({
      tip: 'expert',
      tipData: {
        expert: this.data.class.expert
      }
    })
  },
  share: function() {
    var version = '1.0.0'
    wx.getSystemInfo({
      success: function(res) {
        version = res.SDKVersion
      }
    })
    if(version >= '1.2.1') {
      var cid = this.data.class.id
      wx.showShareMenu({
        withShareTicket: false,
        success: function() {
          epclass.shareLogger({
            class_id: cid,
            target: '课程预约',
            action: 'appMessage'
          }, r => {
          })
        }
      })
    } else {
      this.setData({
        tip: 'share',
        tipData: {}
      })
    }
  },
  showGoIndexTip: function(msg) {
    this.setData({
      tip: 'go_index_tip',
      tipData: msg
    })
  },
  showGoLiveTip: function(msg) {
    this.setData({
      tip: 'go_live_tip',
      tipData: msg
    })
  },
  showErrorTip: function(msg) {
    this.setData({
      tip: 'error_tip',
      tipData: msg
    })
  },
  showApplySuccess: function() {
    this.setData({
      class: this.data.class,
      tip: 'apply_success',
      tipData: {
        date: this.data.class.start_at
      }
    })
  },
  showGoRegisterTip: function() {
    this.setData({
      class: this.data.class,
      tip: 'apply_register',
      tipData: {}
    })
  },
  goIndex() {
    navigate.redirect(`/pages/index/index`)
  },
  goReview() {
    navigate.redirect(`/pages/epclass/review/index?cid=` + this.data.class.id)
  },
  goLive() {
    navigate.redirect(`/pages/epclass/live/index?cid=` + this.data.class.id)
  },
  closeExpertTip: function() {
    this.setData({
      tip: 'blank'
    })
  },
  close: function() {
    this.setData({
      tip: 'blank',
      tipData: {}
    })
  },
  closeTip: function() {
    this.setData({
      tip: 'blank',
      tipData: {}
    })
  },
  mall: function() {
    this.setData({
      tip: 'go_mall_tip'
    })
  },
  goMall: function() {
    navigate.to('/pages/mall/index')
  },
  toRegister: function() {
    navigate.to('/pages/register/index?from=' + encodeURIComponent('/pages/epclass/view/index?cid=' + this.data.class.id))
  },
  onShareAppMessage: function() {
    App.jice.track('onshare', {
      shareto: 'friend',
      title: `与专家对话，做A+妈妈！我已经报名参与【${this.data.class.name}】`,
      path: `/pages/epclass/view/index?cid=${this.data.class.id}`
    })
    return {
      title: `与专家对话，做A+妈妈！我已经报名参与【${this.data.class.name}】`,
      desc: `你和A+妈妈的距离，就差了一次跟专家交流的机会。还犹豫什么呢？赶紧来报名【${this.data.class.name}】吧！`,
      path: `/pages/epclass/view/index?cid=${this.data.class.id}`,
      success: () => {
        epclass.shareLogger({
          class_id: this.data.class.id,
          target: '课程预约',
          action: 'appMessage'
        }, r => {
        })
      }
    }
  }
})